package com.ruoyi.system.mongo;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.mongodb.Users;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Pattern;

@Component
public class SysUserDao {
    @Autowired
    private MongoTemplate mongoTemplate;


    public Users queryUserById(String id) {
        Query query=new Query(Criteria.where("id").is(new ObjectId(id)));
        Users one = mongoTemplate.findOne(query, Users.class);
        return one;
    }

    /**
     * 根据学校ID查询列表
     * @param schoolId
     * @return
     */
    public List<Users> queryUsersBySchoolId(ObjectId schoolId) {
        Query query=new Query(Criteria.where("school").is(schoolId));
        query.addCriteria(Criteria.where("role").is(0)); //只查学生
        List<Users> schoolStudents = mongoTemplate.find(query, Users.class);
        return schoolStudents;
    }

    /**
     * 创建对象
     */
    public void saveUser(SysUser test) {
        mongoTemplate.save(test);
    }

    /**
     * 根据用户名查询对象
     * @return
     */
    public SysUser findUserByName(String username) {
        Query query=new Query(Criteria.where("username").is(username));
        SysUser user =  mongoTemplate.findOne(query , SysUser.class);
        return user;
    }

    public Users queryUserByName(String username) {
        Query query=new Query(Criteria.where("username").is(username));
        Users one = mongoTemplate.findOne(query, Users.class);
        return one;
    }

    /**
     * 更新对象
     */
    public void updateTest(SysUser user) {
        Query query=new Query(Criteria.where("id").is(user.getUserId()));
        Update update= new Update().set("username", user.getUserName())
                .set("nikename", user.getNickName());
        //更新查询返回结果集的第一条
        mongoTemplate.updateFirst(query,update,SysUser.class);
        //更新查询返回结果集的所有
        // mongoTemplate.updateMulti(query,update,TestEntity.class);
    }

    /**
     * 删除对象
     * @param id
     */
    public void deleteTestById(Integer id) {
        Query query=new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,SysUser.class);
    }
    /**
    查询所有
     */
    public List<Users> queryUsers(){
        List<Users> users = mongoTemplate.findAll(Users.class);
        return users;
    }
    /**
     查询所有老师
     */
    public List<Users> queryTeachers(String epschool, String nikeName, String phoneNumber){
        Query query = new Query();
        query.addCriteria(Criteria.where("role").is(9));
        query.addCriteria(Criteria.where("school").is(new ObjectId(epschool)));
        if (!StringUtils.isEmpty(nikeName)) {
            Pattern pattern=Pattern.compile("^.*"+nikeName+".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("nikename").regex(pattern));
        }
        if (!StringUtils.isEmpty(phoneNumber)) {
            query.addCriteria(Criteria.where("phoneNum").is(phoneNumber));
        }
        List<Users> users = mongoTemplate.find(query, Users.class);
        return users;
    }
}
